package com.diyunapp.xilinapp.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.WebNoticeBean;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WebNoticeActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back1)
    ImageView goBack1;
    @Bind(R.id.notice_fresh_lv)
    ListView noticeFreshLv;
    @Bind(R.id.notice_refresh_view)
    SwipeRefreshLayout noticeRefreshView;

    private LayoutInflater inflater;
    private NoticeAdapter noticeAdapter;
    private List<WebNoticeBean.DataBean> info = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_notice);
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        ButterKnife.bind(this);
        View headView;
        headView = findViewById(R.id.webnotice_menubar_inc);
        super.MenuWidget(headView);
        noticeAdapter=new NoticeAdapter(this);
        initNotice();
        noticeFreshLv.setAdapter(noticeAdapter);
        noticeRefreshView.setColorSchemeColors(R.color.domain_orange, R.color.green,
                R.color.blue, R.color.yellow);

        noticeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("zhangxiaoxidebug", "myordersSwiperefreshlayout onRefresh...");
                initNotice();
            }
        });

    }

    @OnClick({ R.id.go_back1})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.go_back1:
                finish();
                break;
        }
    }

    public void initNotice() {
        String url = ""+ MainActivity.url+"act=goods&op=gglist&ac_id=1";

        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(10000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        noticeRefreshView.setRefreshing(false);
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");

                            if (result == 200) {
                                info.clear();
                                JSONArray listArray = resultObject.optJSONArray("data");
                                Log.i("zhangxiaoxidebug", "listArray=" + listArray);
                                for (int i = 0; i < listArray.length(); i++) {
                                    JSONObject recordObject = listArray.optJSONObject(i);
                                    String article_id = recordObject.optString("article_id");
                                    String article_title = recordObject.optString("article_title");


                                    info.add(new WebNoticeBean.DataBean(article_id, article_title));
                                }
                                noticeAdapter.setList(info);

                                noticeAdapter.notifyDataSetChanged();

                            } else {
                                toast("当前暂无数据");
                                noticeRefreshView.setVisibility(View.GONE);

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();
                            noticeRefreshView.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        noticeRefreshView.setRefreshing(false);
                        toast("请求失败");
                        noticeRefreshView.setVisibility(View.GONE);
                    }
                });
    }

    class NoticeAdapter extends BaseAdapter {

        private List<WebNoticeBean.DataBean> info = new ArrayList<WebNoticeBean.DataBean>();
        private Context context;

        public NoticeAdapter(Context context) {
            this.context = context;

        }

        public void setList(List<WebNoticeBean.DataBean> list) {
            this.info = list;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return info == null ? 0 : info.size();
        }

        @Override
        public Object getItem(int position) {
            return info == null ? null : info.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder;
            if (null == convertView) {

                convertView = inflater.inflate(R.layout.item_web_notice, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
//            noticeFreshLv.setTag(position);
            viewHolder.linCompanyQq.setTag(position);
            WebNoticeBean.DataBean bean = info.get(position);
            viewHolder.noticeContent.setText(bean.getArticle_title());
            final String article_id=bean.getArticle_id();
viewHolder.linCompanyQq.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(WebNoticeActivity.this, NoticeContentActivity.class);
        intent.putExtra("article_id", article_id);

        startActivity(intent);
    }
});
            return convertView;

        }


        class ViewHolder {
            @Bind(R.id.notice_content)
            TextView noticeContent;
            @Bind(R.id.lin_company_qq)
            LinearLayout linCompanyQq;
            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
